import javax.swing.*;
import java.awt.*;

public class ScrollText extends JPanel {
    private static final long serialVersionUID = 1L;
    private String text = "Scrolling Text";
    private Font font = new Font("Serif", Font.BOLD, 20);
    private int x = 0;

    public ScrollText() {
        Timer timer = new Timer(50, e -> {
            x -= 2;
            if (x < -getTextWidth()) {
                x = getWidth();
            }
            repaint();
        });
        timer.start();
    }

    private int getTextWidth() {
        return getFontMetrics(font).stringWidth(text);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setFont(font);
        g2d.drawString(text, x, 50);
    }

    public static void main(String[] args) {
        new ScrollText();
    }
}